package com.dong.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.dong.web.util.StringUtil;
import com.dong.web.whold.Variables;

public class EncodingFilter implements Filter {

	public void init(FilterConfig config) throws ServletException {
		String vEncoding = config.getInitParameter("encoding");
		if (vEncoding != null) {
			vEncoding = StringUtil.replaceBlank(vEncoding);
			Variables.ENCODING = vEncoding;
		}
	}

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		String encoding = Variables.ENCODING;
		req.setCharacterEncoding(encoding);
		res.setCharacterEncoding(encoding);
		res.setContentType("text/html;charset=" + encoding);
		chain.doFilter(req, res);
	}

	public void destroy() {

	}

}
